<?php
/**
 * 声波设备
 */
namespace org\devices;
use org\Curl;
use think\Facade\Log;

class SoundCoolEasy extends SoundDevice
{  
    private $SoundApiUrl = 'http://v1.webapi.cooleasy.net/SoundApi';
    private $SoundPulseApiUrl = 'http://v1.webapi.cooleasy.net/SoundPulseApi';
    private $token = "51638566594e47429116518657b31c11";
    private $AppScrect = "3OfWqnSbflo0133mF1LV7hoUuw9MobGH";

    // 获取设备规则
    public function getOptions() {
        $result = [
            'status' => true,
            'data' => [],
            'msg' => ''
        ];

        // 控制规则
        $result['data']['ctrl'] = [
            0 => [
                'name' => '通道',
                'value' => [
                    0 => ['name' => '左', 'value'=>'0'],
                    1 => ['name' => '右', 'value'=>'1']
                ]
            ]
        ];
        
        // 计费规则
        $result['data']['fee'] = [
            0 => [
                'name' => '时长',
                'value' => [
                    0 => ['name' => '1分钟', 'value'=>'1'],
                    1 => ['name' => '2分钟', 'value'=>'2'],
                    2 => ['name' => '3分钟', 'value'=>'3']
                ]
            ]
        ];

        return $result;
    }
  
    // 获取启动费用
    public function checkConfig() {  
        $result = ['status' => false, 'data'   => '', 'msg' => '参数错误' ];

        $channelIsValid = false;
        $minsIsValid = false;
        foreach($this->getOptions()['data']['ctrl'] as $key => $val) {
            foreach($val['value'] as $ctrlKey => $ctrlVal) { 
                if($ctrlVal['value'] == $this->config['channels']){
                    $channelIsValid = true;
                }
            }
        }

        foreach($this->getOptions()['data']['fee'] as $key => $val) {
            foreach($val['value'] as $feeKey => $feeVal) { 
                if($feeVal['value'] == $this->config['mins']){
                    $minsIsValid = true;
                }
            }
        } 

        if($channelIsValid && $minsIsValid) {
            $result['status'] = true;
            $result['msg'] = '';
        }

        return $result; 
    }

    // 启动设备
    public function start() {
        $result = ['status' => false, 'data' => '', 'msg'  => '吹风机使用请求失败，请重试！'];

        // 向爽易请求支付声波
        $reqResult = $this->getAccessToken();
        if($reqResult["success"] == 0) {
            $result['data']['errorCode'] = 'cooleasy';
            return $result;
        }

        $result['status'] = true;
        $result['data'] = $reqResult;
        return $result; 
    }

    // 停止设备
    public function stop(){
        $result = [
            'status' => false,
            'data' => [],
            'msg' => ''
        ];
        return $result;
    }

     /**
     * @return array|mixed
     */
    private function getAccessToken()
    {
        $mins  = $this->config['mins'];
        $channels  = $this->config['channels'];
        $sub_id  = $this->config['sub_id'];
        $time = time();

        // 这里需要对参数进行排序，省事了直接写死
        $key=$this->AppScrect."channel=".$channels."machinesn=".$sub_id."mins=".$mins."timestamp=".$time."token=".$this->token;
        $sign = strtoupper(md5($key));

        // 拼接请求参数
        $url = $this->SoundApiUrl.= "?channel=".$channels."&machineSn=".$sub_id."&mins=".$mins."&token=".$this->token."&timeStamp=".$time."&sign=".$sign;

        // 请求使用设备
        $curl = new Curl();
        $re = $curl::post($url, "");
        $re = json_decode($re,true);

        if($re && $re["success"] == 1){
            return $re;
        }

        $re["success"] = 0;
        return $re;
    }
}
